{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 列表如何转为ndarray"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:06:37.515702Z",
     "start_time": "2025-01-06T03:06:37.513111Z"
    }
   },
   "source": [
    "#列表转ndarray\n",
    "import numpy as np\n",
    "\n",
    "list1 = [1, 2, 3, 4]\n",
    "print(list1)\n",
    "print(type(list1))\n",
    "\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:07:58.147501Z",
     "start_time": "2025-01-06T03:07:58.144854Z"
    }
   },
   "cell_type": "code",
   "source": [
    "oneArray = np.array(list1)\n",
    "print(type(oneArray))  #类型是ndarray\n",
    "print(oneArray)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarray与Python原生list运算效率对比"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-05T07:20:39.851848Z",
     "start_time": "2025-02-05T07:20:18.505738Z"
    }
   },
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "\n",
    "#随机1亿个数据\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "print('随机完毕')"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机完毕\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-05T07:20:55.319086Z",
     "start_time": "2025-02-05T07:20:39.862996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.array(a)  # 转换为ndarray\n",
    "print('转换完毕')"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换完毕\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-05T07:21:24.247235Z",
     "start_time": "2025-02-05T07:21:22.428752Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#ndarray的计算效率优于python的list\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.6148746013641357 0.1976325511932373\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-05T07:21:12.814304Z",
     "start_time": "2025-02-05T07:21:12.806694Z"
    }
   },
   "source": [
    "t1 = np.array([1, 2, 3])\n",
    "print(t1)\n",
    "print(type(t1))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:13:50.187443Z",
     "start_time": "2025-01-06T03:13:50.184962Z"
    }
   },
   "source": [
    "print(type(range(10)))  # range返回的是一个range对象，不能直接转换为ndarray\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:14:32.503512Z",
     "start_time": "2025-01-06T03:14:32.499729Z"
    }
   },
   "source": [
    "t3 = np.arange(0, 10, 2)  # 等价于range(0,10,2),返回ndarray\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:16:38.588304Z",
     "start_time": "2025-01-06T03:16:38.585304Z"
    }
   },
   "source": [
    "#二维列表转ndarray\n",
    "import numpy as np\n",
    "\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "\n",
    "twoArray = np.array(list2)\n",
    "print(type(twoArray))\n",
    "print(twoArray)\n",
    "print(list2)  #列表的输出是有逗号的，ndarray的输出是没有逗号的"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1, 2], [3, 4], [5, 6]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "source": "twoArray.tolist()  # ndarray转换为列表",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T03:17:57.564171Z",
     "start_time": "2025-01-06T03:17:57.560037Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4], [5, 6]]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4.3 常用属性"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:18:32.097951Z",
     "start_time": "2025-01-06T03:18:32.094721Z"
    }
   },
   "source": [
    "#看ndarray的各种属性\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "\n",
    "twoArray = np.array(list2)\n",
    "# 获取数组的维度( 注意： 与函数的参数很像) \n",
    "print(twoArray.ndim)\n",
    "\n",
    "# 形状（行，列） (0 axis,1 axis)\n",
    "print(twoArray.shape)\n",
    "\n",
    "# 有多少个元素\n",
    "print(twoArray.size)\n",
    "# 数据类型\n",
    "print(twoArray.dtype)  #默认int32,4个字节，数据范围 -21亿到21亿"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n",
      "int32\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "cell_type": "markdown",
   "source": "# 4.4 调整数组的形状",
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:22:29.721942Z",
     "start_time": "2025-01-06T03:22:29.718439Z"
    }
   },
   "source": [
    "four = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print(four)\n",
    "# 修改的是原有的\n",
    "four1 = four\n",
    "print(id(four))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "1922834210736\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T03:22:52.644016Z",
     "start_time": "2025-01-06T03:22:52.639924Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four.shape = (3, 2)  # 改变形状\n",
    "print(id(four))\n",
    "print(id(four1))\n",
    "print(four)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1922834210736\n",
      "1922834210736\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "# 返回一个新的数组，reshape后id和之前的不一样\n",
    "four2 = four.reshape(2, 3)\n",
    "print(four)\n",
    "print(id(four))\n",
    "print(id(four2))\n",
    "four2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T03:25:03.636706Z",
     "start_time": "2025-01-06T03:25:03.633193Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "1922834210736\n",
      "1925835961904\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:09:30.767637Z",
     "start_time": "2025-01-06T06:09:30.765134Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将多维变成一维数组\n",
    "five = four.reshape((6,), order='C')\n",
    "# 默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six = four.flatten()  # 展开成一维数组\n",
    "print(five)\n",
    "print('-' * 50)\n",
    "print(six)\n",
    "\n",
    "# print(five.reshape(3,2))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "--------------------------------------------------\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:06:51.912389Z",
     "start_time": "2025-01-06T06:06:51.906572Z"
    }
   },
   "cell_type": "code",
   "source": [
    "seven = five.reshape(3, 2)  # 改变形状\n",
    "seven"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "# 拓展：数组的形状\n",
    "t = np.arange(24)  #和列表的range函数一样，生成一个数组\n",
    "print(t)\n",
    "print(f'shape{t.shape}')\n",
    "print(t.ndim)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((4, 6))\n",
    "print(t1)\n",
    "print(t1.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T06:10:43.536101Z",
     "start_time": "2025-01-06T06:10:43.533238Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "shape(24,)\n",
      "1\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:12:04.108913Z",
     "start_time": "2025-01-06T06:12:04.106322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 转成三维\n",
    "#最前面的是零轴，轴越小，越靠外，最小的轴是最外层\n",
    "# 0轴，1轴，2轴\n",
    "t2 = t.reshape((2, 3, 4))\n",
    "print(t2)\n",
    "print(t2.shape)\n",
    "print(t2.ndim)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n",
      "3\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:14:58.130527Z",
     "start_time": "2025-01-06T06:14:58.127512Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t3 = t.reshape((2, 3, 2, 2))\n",
    "print(t3)\n",
    "print(t3.shape)\n",
    "print(t3.ndim)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[ 0  1]\n",
      "   [ 2  3]]\n",
      "\n",
      "  [[ 4  5]\n",
      "   [ 6  7]]\n",
      "\n",
      "  [[ 8  9]\n",
      "   [10 11]]]\n",
      "\n",
      "\n",
      " [[[12 13]\n",
      "   [14 15]]\n",
      "\n",
      "  [[16 17]\n",
      "   [18 19]]\n",
      "\n",
      "  [[20 21]\n",
      "   [22 23]]]]\n",
      "(2, 3, 2, 2)\n",
      "4\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "cell_type": "markdown",
   "source": [
    "4.5 数组转换为列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:20:18.912164Z",
     "start_time": "2025-01-06T06:20:17.898740Z"
    }
   },
   "source": [
    "#将数组转换为列表 tolist\n",
    "a = np.array([9, 12, 88, 14, 25])\n",
    "print(type(a))\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5 NumPy的数据类型"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:26:18.517129Z",
     "start_time": "2025-01-06T06:26:18.514659Z"
    }
   },
   "source": [
    "import random\n",
    "\n",
    "f = np.array([1, 2, 3, 4, 127], dtype=np.int8)  # 返回数组中每个元素的字节单位长度,dtype设置数据类型\n",
    "print(f.itemsize)  # 1 np.int16(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "int8\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:26:20.404048Z",
     "start_time": "2025-01-06T06:26:20.401497Z"
    }
   },
   "cell_type": "code",
   "source": "f[4] = f[4] + 1  #溢出",
   "outputs": [],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:26:22.068893Z",
     "start_time": "2025-01-06T06:26:22.064752Z"
    }
   },
   "cell_type": "code",
   "source": "f[4]",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-128"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:24:26.316594Z",
     "start_time": "2025-01-06T06:24:26.313092Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "print(f1.itemsize)  #显示字节数\n",
    "print('-' * 50)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "8\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:28:20.006690Z",
     "start_time": "2025-01-06T06:28:20.003778Z"
    }
   },
   "cell_type": "code",
   "source": "random.random()  # 随机生成0-1之间的小数",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7286964288500806"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "cell_type": "code",
   "source": [
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(), 2))\n",
    "# np.float16 半精度\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "print(arr)\n",
    "print(arr.itemsize)\n",
    "print(arr.dtype)\n",
    "# 取小数点后两位\n",
    "print(np.round(arr, 2))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T06:29:54.858270Z",
     "start_time": "2025-01-06T06:29:54.855012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.84\n",
      "[0.11206566 0.91793226 0.84856337 0.98588934 0.95315264 0.14406265\n",
      " 0.35482501 0.86209711 0.91629262 0.57545547]\n",
      "8\n",
      "float64\n",
      "[0.11 0.92 0.85 0.99 0.95 0.14 0.35 0.86 0.92 0.58]\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组和数的计算"
   ]
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:33:15.915461Z",
     "start_time": "2025-01-06T06:33:15.657011Z"
    }
   },
   "cell_type": "code",
   "source": "# [1,2,3]+5",
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate list (not \"int\") to list",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[46], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43m[\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241;43m+\u001B[39;49m\u001B[38;5;241;43m5\u001B[39;49m\n",
      "\u001B[1;31mTypeError\u001B[0m: can only concatenate list (not \"int\") to list"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:33:45.875170Z",
     "start_time": "2025-01-06T06:33:45.872134Z"
    }
   },
   "source": [
    "# 由于numpy的广播机机制在运算过程中，加减乘除的值被广播到所有的元素上面\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(t1)\n",
    "print(\"-\" * 20)\n",
    "t2 = t1.tolist()\n",
    "print(t1 + 2)\n",
    "# print(t2+2) 不能对列表进行直接加整数操作\n",
    "print(\"-\" * 20)\n",
    "print(t1 * 2)\n",
    "print(\"-\" * 20)\n",
    "print(t1 / 2)\n",
    "\n",
    "#无论多少维的ndarray都可以直接和一个常数进行运算"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "--------------------\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "cell_type": "markdown",
   "source": [
    "ndarray与ndarray的运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:35:04.889124Z",
     "start_time": "2025-01-06T06:35:04.884450Z"
    }
   },
   "source": [
    "#形状相同,才可以进行加减乘除\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print('-' * 50)\n",
    "print(t1 + t2)\n",
    "print('-' * 50)\n",
    "print(t1 * t2)  #是否是矩阵乘法？不是\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[100 101 102 103]\n",
      " [104 105 106 107]\n",
      " [108 109 110 111]\n",
      " [112 113 114 115]\n",
      " [116 117 118 119]\n",
      " [120 121 122 123]]\n",
      "--------------------------------------------------\n",
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "--------------------------------------------------\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:35:34.585989Z",
     "start_time": "2025-01-06T06:35:34.577686Z"
    }
   },
   "source": [
    "#shape不同，不能进行运算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(6).reshape((3, 6))\n",
    "print(t1)\n",
    "print(t2)\n",
    "print(t1 - t2)"
   ],
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot reshape array of size 6 into shape (3,6)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[50], line 3\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m#shape不同，不能进行运算\u001B[39;00m\n\u001B[0;32m      2\u001B[0m t1 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m24\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape((\u001B[38;5;241m4\u001B[39m,\u001B[38;5;241m6\u001B[39m)) \n\u001B[1;32m----> 3\u001B[0m t2 \u001B[38;5;241m=\u001B[39m \u001B[43mnp\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43marange\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m6\u001B[39;49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mreshape\u001B[49m\u001B[43m(\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m6\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m \n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(t1)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(t2) \n",
      "\u001B[1;31mValueError\u001B[0m: cannot reshape array of size 6 into shape (3,6)"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:36:09.942368Z",
     "start_time": "2025-01-06T06:36:09.938890Z"
    }
   },
   "source": [
    "#一维数组和二维数组进行运算时，一维的元素个数和列数相等\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(6).reshape((1, 6))\n",
    "print(t2.shape)\n",
    "print(t1)\n",
    "print(t2)\n",
    "t1 - t2\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 6)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0 1 2 3 4 5]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0,  0,  0,  0,  0],\n",
       "       [ 6,  6,  6,  6,  6,  6],\n",
       "       [12, 12, 12, 12, 12, 12],\n",
       "       [18, 18, 18, 18, 18, 18]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:39:49.695186Z",
     "start_time": "2025-01-06T06:39:49.691726Z"
    }
   },
   "cell_type": "code",
   "source": "(t1 - t2).shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 6)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 52
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:40:17.032932Z",
     "start_time": "2025-01-06T06:40:17.029395Z"
    }
   },
   "source": [
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(4).reshape((4, 1))\n",
    "print(t2)\n",
    "print(t1)\n",
    "t1 - t2\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 5,  6,  7,  8,  9, 10],\n",
       "       [10, 11, 12, 13, 14, 15],\n",
       "       [15, 16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[[ 0,  0,  0,  0],\n        [ 0,  0,  0,  0],\n        [ 0,  0,  0,  0]],\n\n       [[12, 12, 12, 12],\n        [12, 12, 12, 12],\n        [12, 12, 12, 12]]])"
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#随机3维数组\n",
    "a = np.arange(24).reshape((2, 3, 4))\n",
    "b = np.arange(12).reshape((1, 3, 4))\n",
    "a - b"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-04T08:52:42.010386600Z",
     "start_time": "2024-07-04T08:52:41.998588100Z"
    }
   }
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "结论：ndim要相同，可以某一个轴的size不同，但是其中一个ndarray必须对应的轴的size为1"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练习轴"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:48:37.357685Z",
     "start_time": "2025-01-06T06:48:37.354195Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(a)\n",
    "print(\"-\" * 20)\n",
    "print(np.sum(a, axis=0))  # [5 7 9]，按哪个轴求和，哪个轴没了\n",
    "print(\"-\" * 20)\n",
    "print(np.sum(a, axis=1))  # [ 6 15]\n",
    "print(\"-\" * 20)\n",
    "print(np.sum(a))\n",
    "print(\"-\" * 20)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------\n",
      "[5 7 9]\n",
      "--------------------\n",
      "[ 6 15]\n",
      "--------------------\n",
      "21\n",
      "--------------------\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "cell_type": "code",
   "source": [
    "a = np.arange(24).reshape((2, 3, 4))\n",
    "b = np.sum(a, axis=0)\n",
    "print(\"-\" * 20)\n",
    "print(b.shape)\n",
    "c = np.sum(a, axis=1)\n",
    "print(\"-\" * 20)\n",
    "print(c.shape)\n",
    "d = np.sum(a, axis=2)\n",
    "print(\"-\" * 20)\n",
    "print(d.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T06:50:44.374336Z",
     "start_time": "2025-01-06T06:50:44.360732Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "(3, 4)\n",
      "--------------------\n",
      "(2, 4)\n",
      "--------------------\n",
      "(2, 3)\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:52:29.300166Z",
     "start_time": "2025-01-06T06:52:29.297307Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[0], a)\n",
    "\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "\n",
    "print(a[2:8:2])  #切片是左闭右开\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[2 4 6]\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:53:03.976426Z",
     "start_time": "2025-01-06T06:53:03.972744Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(t1)\n",
    "print('*' * 20)\n",
    "print(t1[1])  # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*' * 20)\n",
    "print(t1[1:])  # 取连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[1:3, :])  # 取连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2, 3]])  # 取不连续的多行"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "cell_type": "code",
   "source": [
    "#看列表\n",
    "t2 = np.arange(24).reshape(4, 6).tolist()\n",
    "t2[[0, 2, 3]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T06:54:08.928704Z",
     "start_time": "2025-01-06T06:54:08.919184Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list indices must be integers or slices, not list",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[59], line 3\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;66;03m#看列表\u001B[39;00m\n\u001B[0;32m      2\u001B[0m t2 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m24\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape(\u001B[38;5;241m4\u001B[39m,\u001B[38;5;241m6\u001B[39m)\u001B[38;5;241m.\u001B[39mtolist()\n\u001B[1;32m----> 3\u001B[0m \u001B[43mt2\u001B[49m\u001B[43m[\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m]\u001B[49m\n",
      "\u001B[1;31mTypeError\u001B[0m: list indices must be integers or slices, not list"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "cell_type": "code",
   "source": [
    "print(t1[:, 1])  # 取一列\n",
    "print('*' * 20)\n",
    "print(t1[:, 1:])  # 连续的多列\n",
    "print('*' * 20)\n",
    "print(t1[:, [0, 2, 3]])  # 取不连续的多列\n",
    "print('*' * 20)\n",
    "print(t1[2, 3])  # # 取某一个值,三行四列  py是t1[2][3]\n",
    "print('*' * 20)\n",
    "print(t1[[0, 1, 1], [0, 1, 3]])  # 取多个不连续的值，[[行，行。。。],[列，列。。。]]\n",
    "\n",
    "t1[1:3, 1:4]  #取1-3行，1-4列"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T06:55:54.209623Z",
     "start_time": "2025-01-06T06:55:54.205104Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n",
      "[0 7 9]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 7,  8,  9],\n",
       "       [13, 14, 15]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 60
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 修改值"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:25:33.155422Z",
     "start_time": "2025-01-06T07:25:33.151228Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "print(t)\n",
    "print(id(t))\n",
    "print('-' * 50)\n",
    "# # 修改某一行的值\n",
    "# t[1,:]=0\n",
    "#\n",
    "# # 修改某一列的值\n",
    "# t[:,1]=0\n",
    "#\n",
    "# # 修改连续多行\n",
    "# t[1:3,:]=0\n",
    "#\n",
    "# # 修改连续多列\n",
    "# t[:,1:4]=0\n",
    "#\n",
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:3,2:5]=0\n",
    "#\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0,1],[1,3]]=0\n",
    "\n",
    "# 可以根据条件修改，比如讲小于10的值改掉\n",
    "# t[t<10]=0\n",
    "\n",
    "# 使用逻辑判断\n",
    "# np.logical_and\t& # np.logical_or\t|\n",
    "# np.logical_not\t\t~\n",
    "# t[(t>2)&(t<6)]=0\t# 逻辑与，and\n",
    "# t[(t<2)|(t>6)]=0\t# 逻辑或，or\n",
    "# t[~(t>6)]=0\t# 逻辑非\n",
    "# print(t)\n",
    "t = t.clip(10, 18)\n",
    "print(id(t))\n",
    "t\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "1922834742320\n",
      "--------------------------------------------------\n",
      "1922834732720\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10, 10, 10, 10],\n",
       "       [10, 10, 10, 10, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 18, 18, 18, 18, 18]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:30:23.852792Z",
     "start_time": "2025-01-06T07:30:23.849286Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "t < 10"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:27:46.977195Z",
     "start_time": "2025-01-06T07:27:46.974300Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#python的三目运算\n",
    "a = 10\n",
    "b = 15\n",
    "c = a if a > b else b\n",
    "c"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 71
  },
  {
   "cell_type": "code",
   "source": [
    "# # 拓 展\n",
    "# # 三目运算（ np.where(condition, x, y)满足条件(condition)，输出x，不满足输出y。)）\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(score)\n",
    "result = np.where(score < 80, True, False)  #类似于if else\n",
    "print(result)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T07:28:48.134711Z",
     "start_time": "2025-01-06T07:28:48.131604Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[[False False]\n",
      " [False False]\n",
      " [ True False]]\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:29:32.080432Z",
     "start_time": "2025-01-06T07:29:32.077216Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score[result] = 100\n",
    "score"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 80,  88],\n",
       "       [ 82,  81],\n",
       "       [100,  81]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 73
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据的添加，删除与去重"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:31:22.280480Z",
     "start_time": "2025-01-06T07:31:22.276928Z"
    }
   },
   "source": [
    "# 1. numpy.append 函数在数组的末尾添加值。 追加操作会分配整个数组，并把原来的数组复制到新数组中。\n",
    "# 此外，输入数组的维度必须匹配否则将生成ValueError。\n",
    "'''\n",
    "参 数 说 明 ：                                                                                                     arr： 输 入 数 组                                                                                                 values：要向arr添加的值，需要和arr形状相同（除了要添加的轴）                                                            axis：默认为 None。当axis无定义时，是横向加成，返回总是为一维数组！当axis有定义的时候，分别为0和1的时候。当\n",
    "axis有定义的时候，分别为0和1的时候（列数要相同）。当axis为1时，数组是加在右边（行数要相同）。\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print(' 向 数 组 添 加 元 素 ：')\n",
    "print(np.append(a, [7, 8, 9]))  #ndarray会被展平\n",
    "print('\\n')\n",
    "print(a)\n",
    "print('-' * 50)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:31:54.180826Z",
     "start_time": "2025-01-06T07:31:54.178039Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.array([[7, 8, 9]])\n",
    "print(b.ndim)\n",
    "print(b)\n",
    "print('-' * 50)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "[[7 8 9]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 77
  },
  {
   "cell_type": "code",
   "source": [
    "print('沿轴 0 添加元素：')  #往哪个轴添加，那个轴size就变大\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "print('\\n')\n",
    "print(a)\n",
    "print('沿轴 1 添加元素：')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n",
    "print(a)\n",
    "print('-' * 50)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T07:32:42.146703Z",
     "start_time": "2025-01-06T07:32:42.143772Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 78
  },
  {
   "cell_type": "code",
   "source": [
    "# # 2. numpy.insert\n",
    "# # 函数在给定索引之前，沿给定轴在输入数组中插入值。# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# # 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 1, [11, 22]))\n",
    "print('-' * 50)\n",
    "print(np.insert(a, 1, [11, 12], axis=0))  #那个轴插入，哪个轴size变大\n",
    "# print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "\n",
    "print('沿轴  0 广播：')\n",
    "print(np.insert(a, 1, 11, axis=0))\n",
    "print('\\n')\n",
    "\n",
    "print('沿轴  1 广播：')\n",
    "print(np.insert(a, 1, [1, 2, 5], axis=1))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T07:33:44.894297Z",
     "start_time": "2025-01-06T07:33:44.890937Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1 11 22  2  3  4  5  6]\n",
      "--------------------------------------------------\n",
      "[[ 1  2]\n",
      " [11 12]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "沿轴  0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴  1 广播：\n",
      "[[1 1 2]\n",
      " [3 2 4]\n",
      " [5 5 6]]\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "删除第一行：\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "删除第一列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n"
     ]
    }
   ],
   "execution_count": 80,
   "source": [
    "#numpy.delete 函数返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样，\n",
    "# 如果未提供轴参数， 则输入数组将展开。\n",
    "'''\n",
    "参 数 说 明 ： arr： 输 入 数 组\n",
    "obj：可以被切片，整数或者整数数组，表明要从输入数组删除的子数组axis：\n",
    "沿着它删除给定子数组的轴，如果未提供，则输入数组会被展开'''\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a, 5))\n",
    "print('\\n')\n",
    "print(a)\n",
    "print('删除第一行：')\n",
    "print(np.delete(a, 1, axis=0))\n",
    "print('\\n')\n",
    "\n",
    "print('删除第一列：')\n",
    "print(np.delete(a, 1, axis=1))"
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 9 8 2]\n",
      "\n",
      "\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([2, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 84,
   "source": [
    "# numpy.unique 函数用于去除数组中的重复元素。\n",
    "'''\n",
    "arr：输入数组，如果不是一维数组则会展平\n",
    "return_index：如果为true，返回新列表元素在旧列表中的位置（下标），并以列表形式储\n",
    "return_inverse：如果为true，返回旧列表元素在新列表中的位置（下标），并以列表形式储\n",
    "return_counts：如果为true，返回去重数组中的元素在原数组中的出现次数\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 9, 8, 2])\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "#去除重复元素，输出的是有序序列\n",
    "# print ('第一个数组的去重值：')\n",
    "# u = np.unique(a)\n",
    "# print (u)\n",
    "# print ('\\n')\n",
    "\n",
    "# print ('去重数组的索引数组：')\n",
    "# u,indices = np.unique(a, return_index = True)\n",
    "# print(u)\n",
    "# print (indices)\n",
    "# print ('\\n')\n",
    "\n",
    "# print ('我们可以看到每个和原数组下标对应的数值：')\n",
    "# print ('去重数组的下标：')\n",
    "# u,indices = np.unique(a,return_inverse = True)\n",
    "# print (u)\n",
    "# print (indices)\n",
    "# print ('\\n')\n",
    "\n",
    "print('返回去重元素的重复数量：')\n",
    "u, indices = np.unique(a, return_counts=True)\n",
    "print(u)\n",
    "print(indices)\n",
    "u"
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的数学计算"
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:42:29.340519Z",
     "start_time": "2025-01-06T07:42:29.335367Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "print(score)\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2. 获取某一个轴上的数据最大值，对应的轴就消失\n",
    "result = np.max(score, axis=1)\n",
    "print(result)\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(result)\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score, axis=0)\n",
    "print(result)\n",
    "# 5. 数据的比较\n",
    "result = np.maximum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(result)\n",
    "result = np.minimum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result)\n",
    "result = np.maximum([-2, -1, 4, 1, 2], [1, 2, 3, 4, 5])\n",
    "print(result)\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "\n",
    "\n",
    "'''通用函数：\n",
    "numpy.sqrt(array)\t平方根函数\n",
    "numpy.exp(array)\te^array[i]的数组\n",
    "numpy.abs/fabs(array)\t计算绝对值\n",
    "numpy.square(array)\t计算各元素的平方 等于array\t2\n",
    "numpy.log/log10/log2(array)\t计算各元素的各种对数\n",
    "numpy.sign(array)\t计算各元素正负号\n",
    "numpy.isnan(array)\t计算各元素是否为NaN\n",
    "numpy.isinf(array)\t计算各元素是否为NaN\n",
    "numpy.cos/cosh/sin/sinh/tan/tanh(array) 三角函数\n",
    "numpy.modf(array)\t将array中值得整数和小数分离，作两个数组返回\n",
    "numpy.ceil(array)\t向上取整,也就是取比这个数大的整数\n",
    "numpy.floor(array)\t向下取整,也就是取比这个数小的整数\n",
    "numpy.rint(array)\t四舍五入\n",
    "numpy.trunc(array)\t向0取整\n",
    "numpy.cos(array)\t正弦值\n",
    "numpy.sin(array)\t余弦值\n",
    "numpy.tan(array)\t正切值\n",
    "\n",
    "numpy.add(array1,array2)\t元素级加法\n",
    "numpy.subtract(array1,array2)\t元素级减法\n",
    "numpy.multiply(array1,array2)\t元素级乘法\n",
    "numpy.divide(array1,array2)\t元素级除法 array1./array2\n",
    "numpy.power(array1,array2)\t元素级指数 array1.^array2\n",
    "numpy.maximum/minimum(array1,aray2) 元素级最大值\n",
    "numpy.fmax/fmin(array1,array2)\t元素级最大值，忽略NaN\n",
    "numpy.mod(array1,array2)\t元素级求模\n",
    "numpy.copysign(array1,array2)\t将第二个数组中值得符号复制给第一个数组中值\n",
    "numpy.greater/greater_equal/less/less_equal/equal/not_equal (array1,array2)\n",
    "元素级比较运算，产生布尔数组\n",
    "numpy.logical_end/logical_or/logic_xor(array1,array2)元素级的真值逻辑运算\n",
    "'''\n",
    "\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "88\n",
      "[88 82 81]\n",
      "75\n",
      "[75 81]\n",
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[1 2 4 4 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'通用函数：\\nnumpy.sqrt(array)\\t平方根函数\\nnumpy.exp(array)\\te^array[i]的数组\\nnumpy.abs/fabs(array)\\t计算绝对值\\nnumpy.square(array)\\t计算各元素的平方 等于array\\t2\\nnumpy.log/log10/log2(array)\\t计算各元素的各种对数\\nnumpy.sign(array)\\t计算各元素正负号\\nnumpy.isnan(array)\\t计算各元素是否为NaN\\nnumpy.isinf(array)\\t计算各元素是否为NaN\\nnumpy.cos/cosh/sin/sinh/tan/tanh(array) 三角函数\\nnumpy.modf(array)\\t将array中值得整数和小数分离，作两个数组返回\\nnumpy.ceil(array)\\t向上取整,也就是取比这个数大的整数\\nnumpy.floor(array)\\t向下取整,也就是取比这个数小的整数\\nnumpy.rint(array)\\t四舍五入\\nnumpy.trunc(array)\\t向0取整\\nnumpy.cos(array)\\t正弦值\\nnumpy.sin(array)\\t余弦值\\nnumpy.tan(array)\\t正切值\\n\\nnumpy.add(array1,array2)\\t元素级加法\\nnumpy.subtract(array1,array2)\\t元素级减法\\nnumpy.multiply(array1,array2)\\t元素级乘法\\nnumpy.divide(array1,array2)\\t元素级除法 array1./array2\\nnumpy.power(array1,array2)\\t元素级指数 array1.^array2\\nnumpy.maximum/minimum(array1,aray2) 元素级最大值\\nnumpy.fmax/fmin(array1,array2)\\t元素级最大值，忽略NaN\\nnumpy.mod(array1,array2)\\t元素级求模\\nnumpy.copysign(array1,array2)\\t将第二个数组中值得符号复制给第一个数组中值\\nnumpy.greater/greater_equal/less/less_equal/equal/not_equal (array1,array2)\\n元素级比较运算，产生布尔数组\\nnumpy.logical_end/logical_or/logic_xor(array1,array2)元素级的真值逻辑运算\\n'"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 85
  },
  {
   "cell_type": "code",
   "source": [
    "# 6. 求平均值\n",
    "result = np.mean(score)  # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score, axis=0)  # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(arr)\n",
    "print(arr.cumsum(axis=0))\n",
    "'''\n",
    "[1, 2, 3]------>\t|1\t|2\t|3\t|\n",
    "[4, 5, 6]------>\t|5=1+4 |7=2+5 |9=3+6|\n",
    "'''\n",
    "print(arr.cumsum(axis=1))\n",
    "'''\n",
    "[1, 2, 3]------>\t|1\t|2+1\t|3+2+1\t|\n",
    "[4, 5, 6]------>\t|4\t|4+5\t|4+5+6\t|\n",
    "'''\n",
    "\n",
    "\n",
    "\n",
    "# 拓展：方差var, 协方差cov, 计算平均值 average, 计算中位数 median"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T07:51:13.042363Z",
     "start_time": "2025-01-06T07:51:13.038355Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n[1, 2, 3]------>\\t|1\\t|2+1\\t|3+2+1\\t|\\n[4, 5, 6]------>\\t|4\\t|4+5\\t|4+5+6\\t|\\n'"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 86
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1] [75 81]\n",
      "[2.94392029 3.29983165]\n",
      "13\n"
     ]
    }
   ],
   "source": [
    "# 8. argmin求最小值索引\n",
    "result = np.argmin(score, axis=0)\n",
    "res = np.min(score, axis=0)  #这样我们就可以知道最小的81是第二排的，是从前往后遍历的\n",
    "print(result, res)\n",
    "\n",
    "# 9. 求每一列的标准差（这里是总体标准差）\n",
    "# 标准差是一组数据平均值分散程度的一种度量。一个较大的标准差，代表大部分数值和其平均值之间差异较大；\n",
    "# 一个较小的标准差，代表这些数据较接近平均值反应出数据的波动稳定情况，越大表示波动越大，越不稳定。\n",
    "result = np.std(score, axis=0)\n",
    "print(result)\n",
    "\n",
    "# 10. 极 值\n",
    "result = np.ptp(score, axis=None)  #就是最大值和最小值的差\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T03:12:41.644301600Z",
     "start_time": "2024-07-05T03:12:41.633350300Z"
    }
   }
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:57:14.041347Z",
     "start_time": "2025-01-06T07:57:14.037904Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(30).reshape(10, 3)\n",
    "np.mean(t, axis=0)"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13.5, 14.5, 15.5])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 87
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的拼接"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:00:53.139963Z",
     "start_time": "2025-01-06T08:00:53.136818Z"
    }
   },
   "source": [
    "# 有的时候我们需要将两个数据加起来一起研究分析，我们就可以将其进行拼接然后分析\n",
    "import numpy as np\n",
    "\n",
    "# 1. 根据轴连接的数组序列，concatenate没有改变数组的维度\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "c = np.array([[9, 10], [11, 12]])\n",
    "# # 要求a,b两个数组的维度相同\n",
    "print('沿轴 0 连接两个数组：')\n",
    "print(np.concatenate((a, b), axis=0))  #写哪个轴，哪个轴就变化\n",
    "print('-' * 50)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:02:32.981999Z",
     "start_time": "2025-01-06T08:02:32.978297Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('沿轴 1 连接两个数组：')\n",
    "print(np.concatenate((a, b,c), axis=1))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 1 连接两个数组：\n",
      "[[ 1  2  5  6  9 10]\n",
      " [ 3  4  7  8 11 12]]\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:05:06.893414Z",
     "start_time": "2025-01-06T08:05:06.890967Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#堆叠\n",
    "print('-' * 50)\n",
    "print(np.stack((a, b,c), axis=1).shape)  #stack会增加维度"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "(2, 3, 2)\n"
     ]
    }
   ],
   "execution_count": 93
  },
  {
   "cell_type": "code",
   "source": [
    "# 2. 根据轴进行堆叠，2维会变为3维\n",
    "arrays = [np.arange(12).reshape(3, 4) for _ in range(10)]\n",
    "# print(arrays)\n",
    "print(len(arrays))\n",
    "print('沿轴 0 连接两个数组：')\n",
    "result0 = np.stack(arrays, axis=0)\n",
    "print(result0.shape)  #(10,3,4)\n",
    "# print (result0)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:08:58.409989Z",
     "start_time": "2025-01-06T08:08:58.406848Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "沿轴 0 连接两个数组：\n",
      "(10, 3, 4)\n"
     ]
    }
   ],
   "execution_count": 98
  },
  {
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "print('沿轴 1 连接两个数组：')\n",
    "print(arrays[0])\n",
    "result1 = np.stack(arrays, axis=1)\n",
    "print(result1.shape)  #(3,10,4)\n",
    "# print(result1)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:10:49.743872Z",
     "start_time": "2025-01-06T08:10:49.739932Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "沿轴 1 连接两个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "(3, 10, 4)\n"
     ]
    }
   ],
   "execution_count": 99
  },
  {
   "cell_type": "code",
   "source": [
    "print('-' * 50)\n",
    "print('沿轴 2连接两个数组：这里-1和2是等价的')\n",
    "result2 = np.stack(arrays, axis=-1)\n",
    "print(arrays[0])\n",
    "print(result2.shape)\n",
    "# print(result2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:10:52.727164Z",
     "start_time": "2025-01-06T08:10:52.723095Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------------------------------------\n",
      "沿轴 2连接两个数组：这里-1和2是等价的\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "(3, 4, 10)\n"
     ]
    }
   ],
   "execution_count": 100
  },
  {
   "cell_type": "code",
   "source": [
    "\n",
    "\n",
    "# # # 3. 矩阵垂直拼接，vstack没有增加维数，类似于concatenate   ---vertical\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.vstack((v1, v2))\n",
    "print(result)\n",
    "\n",
    "# # # 4. 矩阵水平拼接，类似于concatenate  horizontal\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.hstack((v1, v2))\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:11:30.077732Z",
     "start_time": "2025-01-06T08:11:30.074148Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 101
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分割"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:12:45.084406Z",
     "start_time": "2025-01-06T08:12:45.081587Z"
    }
   },
   "source": [
    "# 1. 将一个数组分割为多个子数组\n",
    "'''\n",
    "参数说明：\n",
    "ary：被分割的数组\n",
    "indices_or_sections：是一个整数，就用该数平均切分，如果是一个数组，为沿轴切分的位置（左开右闭）\n",
    "axis：沿着哪个维度进行切向，默认为0，横向切分。为1时，纵向切分\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "arr = np.arange(12).reshape(4, 3)\n",
    "print(arr)\n",
    "print('将数组分为三个大小相等的子数组：b是一个列表')\n",
    "b = np.split(arr, 3, axis=1) #按那个轴，那个轴发生变化\n",
    "print(b)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "将数组分为三个大小相等的子数组：b是一个列表\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n"
     ]
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:13:15.343335Z",
     "start_time": "2025-01-06T08:13:15.340628Z"
    }
   },
   "cell_type": "code",
   "source": "b[0].shape",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 1)"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 103
  },
  {
   "cell_type": "code",
   "source": [
    "b = np.split(arr, 4, axis=0)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:13:26.843138Z",
     "start_time": "2025-01-06T08:13:26.840165Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]]), array([[ 9, 10, 11]])]\n"
     ]
    }
   ],
   "execution_count": 104
  },
  {
   "cell_type": "code",
   "source": [
    "c = np.hsplit(arr, 3)\n",
    "print(c)  #c是列表\n",
    "print(c[0].shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:13:35.158542Z",
     "start_time": "2025-01-06T08:13:35.155118Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n",
      "(4, 1)\n"
     ]
    }
   ],
   "execution_count": 105
  },
  {
   "cell_type": "code",
   "source": [
    "d = np.random.random((2, 6))\n",
    "print(d)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:14:40.821875Z",
     "start_time": "2025-01-06T08:14:40.818445Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.8993456  0.75174723 0.54821909 0.7931341  0.32570986 0.48264758]\n",
      " [0.52652549 0.28356149 0.64989014 0.78267543 0.75831264 0.4543747 ]]\n"
     ]
    }
   ],
   "execution_count": 106
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原array：\n",
      "[[3. 6. 7. 5. 3. 1.]\n",
      " [1. 0. 6. 1. 8. 6.]]\n"
     ]
    }
   ],
   "source": [
    "# # 2.numpy.hsplit 函数用于水平分割数组，通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "harr = np.floor(10 * np.random.random((2, 6)))\n",
    "print('原array：')\n",
    "print(harr)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T03:39:00.437504900Z",
     "start_time": "2024-07-05T03:39:00.418516600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "source": [
    "# # 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('竖直分割：')\n",
    "b = np.vsplit(a, 2)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:15:36.031861Z",
     "start_time": "2025-01-06T08:15:36.028518Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "竖直分割：\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 108
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy inf和nan"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:17:45.509618Z",
     "start_time": "2025-01-06T08:17:45.506112Z"
    }
   },
   "source": [
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF，最小的负整数是 0x8000 0000。\n",
    "查阅资料后，发现 inf 表示无穷大，需要使用 float(‘inf’) 函数来转化，那么对应的就有\tfloat('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现inf呢？ 比如一个数字除以0，Python中会报错，但是numpy中会是一个inf或者-inf\n",
    "另外还有 nan，这种写法在 pandans 中常见，表示缺失的数据，所以一般用 nan 来表示。任何与其做运算结果都是 nan\n",
    "'''\n",
    "import numpy as np\n",
    "#nan 是not a number的缩写，表示非数字，在numpy中表示一个非数字值，可以用来表示缺失数据\n",
    "# 创建一个nan和inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 109
  },
  {
   "cell_type": "code",
   "source": [
    "# 并 且  np.nan != np.nan\t结果 是TRUE\n",
    "# 所以我们可以使用这两个结合使用判断nan的个数\n",
    "print(np.nan == np.nan)\n",
    "print(True == 1)\n",
    "print(False == 0)\n",
    "np.nan + 1  #nan和其他数运算的结果都是nan"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:18:10.117279Z",
     "start_time": "2025-01-06T08:18:10.113803Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 110
  },
  {
   "cell_type": "code",
   "source": [
    "# --判断数组中为nan的个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:21:45.388227Z",
     "start_time": "2025-01-06T08:21:45.385508Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n"
     ]
    }
   ],
   "execution_count": 115
  },
  {
   "cell_type": "code",
   "source": [
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "# print(np.count_nonzero(t))\n",
    "print(t != t)\n",
    "print('-' * 50)\n",
    "print(np.count_nonzero(t != t))  # 统计nan的个数\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-06T08:21:49.613700Z",
     "start_time": "2025-01-06T08:21:49.610607Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "--------------------------------------------------\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 116
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:22:42.560347Z",
     "start_time": "2025-01-06T08:22:42.557341Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 117
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "(4, 6)\n",
      "2272565048016\n",
      "[ 3. 15. 21.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. 17. 23.]\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "2272565048016\n"
     ]
    }
   ],
   "source": [
    "# ----------练习： 处理数组中nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "print(t.shape)\n",
    "print(id(t))\n",
    "# 遍历每一列，然后判断每一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:, i]\n",
    "\n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为nan的数据拿出来,并计算平均值\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        print(temp_col_not_nan)\n",
    "        # 将nan替换成这一列的平均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "\n",
    "print(t)\n",
    "print(id(t))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-10T02:54:19.051084900Z",
     "start_time": "2024-04-10T02:54:19.035077600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T06:46:17.005377900Z",
     "start_time": "2024-07-05T06:46:16.977107700Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(np.inf == np.inf)\n",
    "np.inf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  2.,  4.,  6.,  8., 10.],\n",
       "       [12., 14., 16., nan, nan, nan],\n",
       "       [24., 26., 28., 30., 32., 34.],\n",
       "       [36., 38., 40., 42., 44., 46.]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.nan和任何数据运算的结果都是nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6).astype('float')\n",
    "t + t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T06:47:27.486868700Z",
     "start_time": "2024-07-05T06:47:27.474758200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0]\n",
      "-inf\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10692\\576235320.py:3: RuntimeWarning: divide by zero encountered in scalar divide\n",
      "  print(arr[0]/arr[1])  #1除0就会得到inf\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([-1, 0])\n",
    "print(arr)\n",
    "print(arr[0] / arr[1])  #1除0就会得到inf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "data": {
      "text/plain": "nan"
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": "np.nan + np.inf",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T06:48:03.998164500Z",
     "start_time": "2024-07-05T06:48:03.988564300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 转置和轴滚动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T06:48:51.981045200Z",
     "start_time": "2024-07-05T06:48:51.973531500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('对换数组：')\n",
    "print(np.transpose(a))\n",
    "print(a)\n",
    "\n",
    "# 与transpose一致\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('转置数组：')\n",
    "print(a.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "(6, 4)\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "re1 = t1.swapaxes(1, 0)\n",
    "\n",
    "print(' 原 数 组 ：')\n",
    "print(t1)\n",
    "print('\\n')\n",
    "print(re1.shape)\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(re1)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-05T06:49:21.033233700Z",
     "start_time": "2024-07-05T06:49:21.021879600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5)\n",
      "--------------------------------------------------\n",
      "(3, 5, 4)\n"
     ]
    }
   ],
   "source": [
    "t3 = np.arange(60).reshape(3, 4, 5)\n",
    "print(t3.shape)\n",
    "print('-' * 50)\n",
    "t3 = np.swapaxes(t3, 1, 2)\n",
    "print(t3.shape)\n",
    "# print(t3) 数据不用记住，不用观察"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-10T03:01:35.658343900Z",
     "start_time": "2024-04-10T03:01:35.627354600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T06:56:04.813442600Z",
     "start_time": "2024-07-05T06:56:04.800462200Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(3, 6, 4, 5)"
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组的轴滚动,swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "a = np.ones((3, 4, 5, 6))\n",
    "# np.rollaxis(a, 2).shape\n",
    "np.rollaxis(a, 3, 1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T06:56:53.925773100Z",
     "start_time": "2024-07-05T06:56:53.909499600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1, 2, 3],\n       [1, 2, 3]])"
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据拷贝，copy()和赋值的区别\n",
    "b = np.array([[1, 2, 3], [1, 2, 3]])\n",
    "a = b.copy()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-05T06:56:57.033771400Z",
     "start_time": "2024-07-05T06:56:57.021594100Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 2 3]\n",
      " [1 2 3]]\n"
     ]
    },
    {
     "data": {
      "text/plain": "array([[1, 2, 3],\n       [1, 2, 3]])"
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0, 0] = 3\n",
    "print(b)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.23367611 0.6272264  0.78609353 0.28895611]\n",
      "  [0.37526288 0.76966567 0.09129868 0.90675475]\n",
      "  [0.43071865 0.62906737 0.02699415 0.69186994]]\n",
      "\n",
      " [[0.4031692  0.03327238 0.5645777  0.58180914]\n",
      "  [0.5438122  0.45401529 0.93533821 0.8071288 ]\n",
      "  [0.03298609 0.07092619 0.28058237 0.72889889]]]\n"
     ]
    }
   ],
   "source": [
    "#随机数生成\n",
    "arr = np.random.rand(2, 3, 4)\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-04-11T01:52:37.469188900Z",
     "start_time": "2024-04-11T01:52:37.406224800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 1. 均匀分布的随"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
